#include <QtGui>
#include <memory.h>
#include "ctrl.h"
#include "ctrlthread.h"
#include "measprog.h"

/*
#include "ipcuty.h"

char SEM_FILE_PATH[] 		= "//bin";
char PROG_SHM_FILE_PATH[]	= "//usr";
char DATA_SHM_FILE_PATH[]	= "//lib";
int	 SEM_ITEM_ID		= 123;
int	 PROG_SHM_ITEM_ID	= 201;
int	 DATA_SHM_ITEM_ID	= 199;
*/

#include "../wrapper.h"

Ctrl* Ctrl::instance = NULL;

Ctrl::Ctrl()
{
//	initMeasEnv();
	ctrlThread = new CtrlThread;
}

/*
//
// Prepare IPC environment, start MEAS process & CTRL thread
//
void Ctrl::initMeasEnv()
{
	semId = sem_create(SEM_FILE_PATH, SEM_ITEM_ID);
	if(sem_p1(semId) == -1){
		qDebug() << "initCtrlThread - sem_p1 error!";
		return;
	}
	if(sem_p2(semId) == -1){
		qDebug() << "initCtrlThread - sem_p1 error!";
		return;
	}

	progShmId = shm_create(PROG_SHM_FILE_PATH, PROG_SHM_ITEM_ID, sizeof(program_s));
	dataShmId = shm_create(DATA_SHM_FILE_PATH, DATA_SHM_ITEM_ID, sizeof(result_s));

	if((progShmPtr = (program_s *)shmat(progShmId, 0, 0)) == (void *)-1){
		qDebug() << "initCtrlThread - program_s shmat error!";
		return;
	}

	if((dataShmPtr = (result_s *)shmat(dataShmId, 0, 0)) == (void *)-1){
		qDebug() << "initCtrlThread - result_s shmat error!";
		return;
	}
	dataShmPtr->isInMeas = true;  // When FALSE, measure will stop

	qDebug() << "q9020 sxm_create:" << dataShmId << "," << progShmId << "," << semId;

	ctrlThread = new CtrlThread;
//	qDebug() << "!!!!!!!!!!!!! ctrlThread = " << ctrlThread;
//	measProcess = new QProcess;	
//	measProcess->start("prog_test");

	pid_t pid = -1;
	if((pid = fork()) == -1){
		qDebug() << "fork error";
	}else if(0 == pid){
		char * const args[] = { "prog_test", NULL };
		if(execv("prog_test", args) == -1){
			qDebug() << "execv error";
			return;
		}
	}

//	ctrlThread = new CtrlThread;

}
*/

/*
void Ctrl::startProgTest()
{
	pid_t pid = -1;
	if((pid = fork()) == -1){
		qDebug() << "fork error";
	}else if(0 == pid){
		char * const args[] = { "prog_test", NULL };
		if(execv("prog_test", args) == -1){
			qDebug() << "execv error";
			return;
		}
	}

//	ctrlThread = new CtrlThread;
}
*/

/*
Ctrl &Ctrl::instance()
{
	static Ctrl ctrl;
	return ctrl;
}
*/

Ctrl* Ctrl::getInstance()
{
	if(NULL == instance){
		instance = new Ctrl;
	}
//	instance->startProgTest();
	return instance;
}


void Ctrl::startCtrlThread()
{
	//ctrlThread = new CtrlThread;
//	startProgTest();
	ctrlThread->start();
//	CtrlThread *ct = new CtrlThread;
//	ct->start();
}



void Ctrl::stopCtrlThread()
{
	delete ctrlThread;
}

program_s* Ctrl::getProgShmPtr()
{
	program_s* p = getTestProgPtr();
	return p;
}

void Ctrl::cpyProgramToShm()
{
	/*
	qDebug() << "in the head of cpyProgramToShm";
	MeasProg mp = MeasProg::instance();	
	program_s *p = mp.getProgPtr();
	qDebug() << p << "," << "item_no=" << p->item_no;
	//p->item_no = 1; //just for debug!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	memcpy(progShmPtr, p, sizeof(program_s));
	
//	qDebug() << "====== prog items ========";
//	qDebug() << p->kinds[0].item[0]
	qDebug() << "in the mid of cpyProgramToShm";
	item_s *ip = NULL;
	for(int ino = 0; ino < p->item_no; ++ino){
		ip = &p->kinds[0].item[ino];
		qDebug() << "======= item" << ino+1 << "=========";
		qDebug() << ip->active;
		qDebug() << ip->brange;
		qDebug() << ip->bvalue;
		qDebug() << ip->mrange;
		qDebug() << ip->mlowlimit;
		qDebug() << ip->mhilimit;
	}
	*/

/*
	MeasProg *pMeasProg = MeasProg::getInstance();
	program_s *pProgram = pMeasProg->getProgPtr();
	memcpy(progShmPtr, pProgram, sizeof(program_s));
*/
	program_s* mpp = getMeasProgPtr();
	program_s* tpp = getTestProgPtr();
	memcpy(tpp, mpp, sizeof(program_s));
}

void Ctrl::cpyProgramFromShm()
{
/*
	MeasProg *pMeasProg = MeasProg::getInstance();
	program_s *pProgram = pMeasProg->getProgPtr();
	memcpy(pProgram, progShmPtr, sizeof(program_s));
*/
	program_s* mpp = getMeasProgPtr();
	program_s* tpp = getTestProgPtr();
	memcpy(mpp, tpp, sizeof(program_s));
}

/*
void Ctrl::sem1_p()
{
	if(sem_p1(semId) == -1){
		qDebug() << "sem1_p error";
	}
}
void Ctrl::sem1_v()
{
	if(sem_v1(semId) == -1){
		qDebug() << "sem1_v error";
	}
}
void Ctrl::sem2_p()
{
	if(sem_p2(semId) == -1){
		qDebug() << "sem2_p error";
	}
}
void Ctrl::sem2_v()
{
	if(sem_v2(semId) == -1){
		qDebug() << "sem2_v error";
	}
}
*/
